<?php
/**
 * A class that will provide necessary information related to an user including authentication
 *
 * @author Kamrul
 */
class UserInformation {
	/**
	 * An array of data for an user
	 *
	 * @var array
	 */
	private $userData = array (
							'id' => -1,
							'name' => 'Some Proper Name',
							'attr' => 'Some Personal Attribute');

	private $dummyUserPass = array(
							'kamrul' => 'nafisa',
							'saikat' => 'shaila',
							'shahid' => 'runa');

	/**
	 * An immaginative private method.
	 * Since it is a private method, it WILL NOT be served as RPC.
	 *
	 * @param integer $min
	 * @param integer $max
	 * @return integer
	 */
	private function reserved($min,$max) {
		return rand($min,$max);
	}

	/**
	 * An immaginative public method.
	 * Since it is a public method, it WILL be served as RPC.
	 * If you want to plug out this method, extend this class with a dummy method.
	 *
	 * This method returns a significative value and must be implemented as a RPC request.
	 *
	 * @param string $param
	 * @return mixed
	 */
	public function giveMeSomeData($param) {
		/*
		 You can have a very complex code here
		 */

		if (array_key_exists($param,$this->userData)) {
			return $this->userData[$param];
		} else {
			throw new Exception('Invalid parameter '.$param);
		}
	}

	private function getStreetNumberName($roadName)	{
		sfContext::getInstance()->getLogger()->debug('UserInformation::getStreetNumberName() --> Enter');
		$streetNum = '';
		$roadParts = split(' ',$roadName);
		$i = 0;
		while(is_numeric(substr($roadParts[$i], 0, 1)) ||
		strcmp(substr($roadParts[$i], 0 , 1), '-') == 0) {
			$streetNum .= $roadParts[$i];
			$i++;
		}
		$streetName = '';

		while($i < count($roadParts )) {
			$streetName .= $roadParts[$i]." ";
			$i++;
		}
		sfContext::getInstance()->getLogger()->debug('streetNumber: '. $streetNum .' streetName: '. $streetName);
		sfContext::getInstance()->getLogger()->debug('UserInformation::getStreetNumberName() --> Exit');
		return array($streetNum, trim($streetName));
	}


	public function getLandmarksByPostcodeAndCategory($postcode, $category_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarksByPostcodeAndCategory() --> Enter');
		sfContext::getInstance()->getLogger()->debug('postcode: '. $postcode .' category_id: '. $category_id);
		try{
			$landmarks = LandmarkPeer::retrieveByPostcodeAndCategory($postcode, $category_id);
			
			if(!$landmarks)	{
				return json_encode("0");
			}

			$php_landmarks = array();
			$index = 0;
			foreach($landmarks as $landmark) {
				$streetNumberName = $this->getStreetNumberName($landmark->getStreet());
				$category = $landmark->getLandmarkCategory();

				$php_landmark = array(
					'id' => $landmark->getId(),
					'name' => $landmark->getName(),
					'streetNumber' => $streetNumberName[0],
					'streetName' => $streetNumberName[1], /// road is changed to street name and number
					'suburb' => $landmark->getSuburb(),
					'postcode' => $landmark->getPostcode(),
					'state' => $landmark->getState(),
					'country' => 'Australia', // THIS VALUE IS HARDCODED FOR THE TIME BEING
					'latitude' => $landmark->getLatitude(),
					'longitude' => $landmark->getLongitude(),
					'categoryId' => $landmark->getCategoryId(),
					'categoryName' => $category->getName()
				);

				$php_landmarks[$index++] = $php_landmark;
			}
			$json_landmarks = JSONHelper::array2json($php_landmarks);
			sfContext::getInstance()->getLogger()->debug('Total landmarks found: '. count($landmarks));
			sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarksByPostcodeAndCategory() --> Exit');
			return $json_landmarks;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getLandmarksByPostcode($postcode) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarksByPostcode() --> Enter');
		sfContext::getInstance()->getLogger()->debug('postcode: '. $postcode);
		return $this->getLandmarksByPostcodeAndCategory($postcode, null);
	}

	public function getLandmarksBySuburbAndCategory($suburb, $category_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarksBySuburbAndCategory() --> Enter');
		sfContext::getInstance()->getLogger()->debug('$suburb: '. $suburb .' category_id: '. $category_id);
		try
		{
			$landmarks = LandmarkPeer::retrieveBySuburbAndCategory($suburb, $category_id);

			if(!$landmarks)	{
				return json_encode("0");
			}

			$php_landmarks = array();
			$index = 0;
			foreach($landmarks as $landmark) {
				$streetNumberName = $this->getStreetNumberName($landmark->getStreet());
				$category = $landmark->getLandmarkCategory();

				$php_landmark = array(
					'id' => $landmark->getId(),
					'name' => $landmark->getName(),
					'streetNumber' => $streetNumberName[0],
					'streetName' => $streetNumberName[1], /// road is changed to street name and number
					'suburb' => $landmark->getSuburb(),
					'postcode' => $landmark->getPostcode(),
					'state' => $landmark->getState(),
					'country' => 'Australia', // THIS VALUE IS HARDCODED FOR THE TIME BEING
					'latitude' => $landmark->getLatitude(),
					'longitude' => $landmark->getLongitude(),
					'categoryId' => $landmark->getCategoryId(),
					'categoryName' => $category->getName()
				);

				$php_landmarks[$index++] = $php_landmark;
			}
			$json_landmarks = JSONHelper::array2json($php_landmarks);
			sfContext::getInstance()->getLogger()->debug('Total landmarks found: '. count($landmarks));
			sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarksBySuburbAndCategory() --> Exit');
			return $json_landmarks;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getLandmarksBySuburb($suburb) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarksBySuburb() --> Enter');
		sfContext::getInstance()->getLogger()->debug('suburb: '. $suburb);
		return $this->getLandmarksBySuburbAndCategory($suburb, null);
	}

	public function getLandmarkById($landmark_id)
	{
		sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarkById() --> Enter');
		sfContext::getInstance()->getLogger()->debug('$landmark_id: '. $landmark_id);
		try{
			$landmark = LandmarkPeer::retrieveByPK($landmark_id);
			$streetNumberName = $this->getStreetNumberName($landmark->getStreet());
			$category = $landmark->getLandmarkCategory();

			if(!$landmark) return json_encode("0");

			$php_landmark = array(
					'id' => $landmark->getId(),
					'name' => $landmark->getName(),
					'streetNumber' => $streetNumberName[0],
					'streetName' => $streetNumberName[1], /// road is changed to street name and number
					'suburb' => $landmark->getSuburb(),
					'postcode' => $landmark->getPostcode(),
					'state' => $landmark->getState(),
					'country' => 'Australia', // THIS VALUE IS HARDCODED FOR THE TIME BEING
					'latitude' => $landmark->getLatitude(),
					'longitude' => $landmark->getLongitude(),
					'categoryId' => $landmark->getCategoryId(),
					'categoryName' => $category->getName()
			);

			sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarkById() --> Exit');
			$json_landmark = JSONHelper::array2json($php_landmark);
			return $json_landmark;
		
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getTasksByUsername($username) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getTasksByUsername() --> Enter');
		sfContext::getInstance()->getLogger()->debug('username: '. $username);
		try{
			$user = sfGuardUserPeer::retrieveByUsername($username);

			if(!$user) return json_encode("0");

			$tasks = $user->getTasks();

			if(!$tasks) return json_encode("0");

			$php_tasks = array();
			$index = 0;
			foreach($tasks as $task) {
				///changed by kamrul

				$priority = TaskPriorityPeer::retrieveByPK($task->getPriorityId()); ///is a must, default is normal
				if(!$priority)
				{
					return json_encode("0");
				}
				$location = $task->getTaskLocation(); ///is a must
				if(!$location)
				{
					return json_encode("0");
				}
				$landmark = $location->getLandmark(); ///optional field

				if($landmark) {
					$streetNumberName = $this->getStreetNumberName($landmark->getStreet());
				}

				/// need to check for null
				if($landmark)
				$category = $landmark->getLandmarkCategory(); ///optional and depends on landmark
				/// need to check for null

				$php_task = array('id' => $task->getId(),
							'name' => $task->getName(),
							'description' => $task->getDescription(),
							'typeId' => $task->getTypeId(),
							'locationId' => $task->getLocationId(),
							'latitude' => $location->getLatitude(),
							'longitude' => $location->getLongitude(),
							'streetNo' => $location->getStreetNumber(),
							'streetName' => $location->getStreetName(),
							'suburb' => $location->getSuburb(),
							'postcode' => $location->getPostcode(),
							'state' => $location->getState(),
							'country' => $location->getCountry(),
							'landmarkId' => $location->getLandmarkId(),
							'landmarkName' => ($landmark) ? $landmark->getName():null,
							'landmarkStreetNo' => ($landmark) ? $streetNumberName[0] : null,
							'landmarkStreetName' => ($landmark) ? $streetNumberName[1] : null,
							'landmarkPostcode' => ($landmark) ? $landmark->getPostcode():null,
							'landmarkSuburb' => ($landmark) ? $landmark->getSuburb():null,	
							'landmarkState' => ($landmark) ? $landmark->getState():null,
							'landmarkCountry' => ($landmark) ? 'Australia' : null,
							'landmarkLatitude'=> ($landmark) ? $landmark->getLatitude():null,
							'landmarkLongitude' => ($landmark) ? $landmark->getLongitude():null,
							'landmarkCategoryId' => ($landmark) ? $landmark->getCategoryId():null,
							'landmarkCategoryName' => ($landmark) ? $category->getName():null,
							'year' => $task->getYear(),
							'month' => $task->getMonth(),
							'day' => $task->getDay(),
							'hour' => $task->getHour(),
							'min' => $task->getMinute(),
							'ampm' => $task->getAmpm(),
							'priorityId' => $task->getPriorityId(),
							'priorityName' => $priority->getName()
				);

				$php_tasks[$index++] = $php_task;
			}

			$json_tasks = JSONHelper::array2json($php_tasks);
			//		sfContext::getInstance()->getLogger()->debug($json_tasks);

			sfContext::getInstance()->getLogger()->debug('UserInformation::getTasksByUsername() --> Exit');
			return $json_tasks;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getTaskByTaskId($task_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getTaskByTaskId() --> Enter');
		sfContext::getInstance()->getLogger()->debug('task_id: '. $task_id);
		try{
			$user = sfGuardUserPeer::retrieveByUsername($username);
			$task = TaskPeer::retrieveByPK($task_id);
			if(!$task) return json_endcode("0");

			$php_task = array('id' => $task->getId(),
							'name' => $task->getName(),
							'description' => $task->getDescription(),
							'typeId' => $task->getTypeId(),
							'locationId' => $task->getLocationId(),
							'year' => $task->getYear(),
							'month' => $task->getMonth(),
							'day' => $task->getDay(),
							'hour' => $task->getHour(),
							'min' => $task->getMinute(),
							'ampm' => $task->getAmpm(),
							'priorityId' => $task->getPriorityId()
			);

			$json_task = JSONHelper::array2json($php_task);
			sfContext::getInstance()->getLogger()->debug('UserInformation::getTaskByTaskId() --> Exit');
			return $json_task;
		}catch (Exception $e) {
			return '-1';
		}
		
	}

	public function getTypeById($type_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getTypeById() --> Enter');
		sfContext::getInstance()->getLogger()->debug('type_id: '. $type_id);
		try{
			$type = TaskTypePeer::retrieveByPK($type_id);

			if(!$type) return json_encode("0");

			$type_name = $type->getName();

			sfContext::getInstance()->getLogger()->debug('UserInformation::getTypeById() --> Exit');
			return $type_name;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getAllTaskTypes() {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getAllTaskTypes() --> Enter');
		try{
			$types = TaskTypePeer::doSelect(new Criteria());

			if(!$types) return json_encode("0");

			$php_task_types = array();
			$index = 0;
			foreach($types as $type) {
				$php_task_type = array('id' => $type->getId(),
							'name' => $type->getName()
				);

				$php_task_types[$index++] = $php_task_type;
			}

			$json_task_types = JSONHelper::array2json($php_task_types);

			sfContext::getInstance()->getLogger()->debug('UserInformation::getAllTaskTypes() --> Exit');
			return $json_task_types;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getAllLandmarks()
	{
		sfContext::getInstance()->getLogger()->debug('UserInformation::getAllLandmarks() --> Enter');
		try{
			$landmarks = LandmarkPeer::doSelect(new Criteria());
			if(!$landmarks)
			{
				return json_encode("0");
			}

			$php_landmarks = array();
			$index = 0;
			foreach($landmarks as $landmark)
			{
				$streetNumberName = $this->getStreetNumberName($landmark->getStreet());
				$category = $landmark->getLandmarkCategory();

				$php_landmark = array(
					'id' => $landmark->getId(),
					'name' => $landmark->getName(),
					'streetNumber' => $streetNumberName[0],
					'streetName' => $streetNumberName[1], /// road is changed to street name and number
					'suburb' => $landmark->getSuburb(),
					'postcode' => $landmark->getPostcode(),
					'state' => $landmark->getState(),
					'country' => 'Australia', // THIS VALUE IS HARDCODED FOR THE TIME BEING
					'latitude' => $landmark->getLatitude(),
					'longitude' => $landmark->getLongitude(),
					'categoryId' => $landmark->getCategoryId(),
					'categoryName' => $category->getName()
				);

				$php_landmarks[$index++] = $php_landmark;
			}
			$json_landmarks = JSONHelper::array2json($php_landmarks);
			sfContext::getInstance()->getLogger()->debug('UserInformation::getAllLandmarks() --> Exit');
			return $json_landmarks;
		}catch (Exception $e) {
			return '-1';
		}
	}


	public function getPriorityById($priority_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getPriorityById() --> Enter');
		sfContext::getInstance()->getLogger()->debug('priority_id: '. $priority_id);
		try{
			$priority = TaskPriorityPeer::retrieveByPK($priority_id);

			if(!$priority) return json_encode("0");

			$priority_name = $priority->getName();
			sfContext::getInstance()->getLogger()->debug('UserInformation::getPriorityById() --> Exit');
			return $priority_name;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getAllTaskPriorities() {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getAllTaskPriorities() --> Enter');
		try{
			$priorities = TaskPriorityPeer::doSelect(new Criteria());

			if(!$priorities) return json_encode("0");

			$php_task_prioritys = array();
			$index = 0;
			foreach($priorities as $priority) {
				$php_task_priority = array('id' => $priority->getId(),
							'name' => $priority->getName()
				);

				$php_task_prioritys[$index++] = $php_task_priority;
			}

			$json_task_prioritys = JSONHelper::array2json($php_task_prioritys);

			sfContext::getInstance()->getLogger()->debug('UserInformation::getAllTaskPriorities() --> Exit');
			return $json_task_prioritys;
		}catch (Exception $e) {
			return '-1';
		}
	}


	public function getTaskLocationByTaskId($task_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getTaskLocationByTaskId() --> Enter');
		sfContext::getInstance()->getLogger()->debug('task_id: '. $task_id);
		try{
			$task = TaskPeer::retrieveByPK($task_id);
			if(!$task) return json_encode("0");
			$location = $task->getTaskLocation();
			if(!$location) return json_encode("0");

			$php_location = array('id' => $location->getId(),
						'latitude' => $location->getLatitude(),
						'longitude' => $location->getLongitude(),
						'streetNo' => $location->getStreetNumber(),
						'streetName' => $location->getStreetName(),
						'suburb' => $location->getSuburb(),
						'postcode' => $location->getPostcode(),
						'state' => $location->getState(),
						'country' => $location->getCountry(),
						'landmarkId' => $location->getLandmarkId()
			);

			$json_location = JSONHelper::array2json($php_location);

			sfContext::getInstance()->getLogger()->debug('UserInformation::getTaskLocationByTaskId() --> Exit');
			return $json_location;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getLandmarkCategoryById($category_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarkCategoryById() --> Enter');
		sfContext::getInstance()->getLogger()->debug('category_id: '. $category_id);
		try
		{
			$category = LandmarkCategoryPeer::retrieveByPK($category_id);

			if(!$category) return json_encode("0");

			$category_name = $category->getName();
			sfContext::getInstance()->getLogger()->debug('UserInformation::getLandmarkCategoryById() --> Exit');
			return $category_name;
		}catch (Exception $e) {
			return '-1';
		}
	}




	public function getAllLandmarkCategories() {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getAllLandmarkCategories() --> Enter');
		try{
			$categories = LandmarkCategoryPeer::doSelect(new Criteria());

			if(!$categories) return json_encode("0");

			$php_location_categories = array();
			$index = 0;
			foreach($categories as $category) {
				$php_location_category = array('id' => $category->getId(),
							'name' => $category->getName()
				);

				$php_location_categories[$index++] = $php_location_category;
			}

			$json_location_categories = JSONHelper::array2json($php_location_categories);

			sfContext::getInstance()->getLogger()->debug('UserInformation::getAllLandmarkCategories() --> Exit');
			return $json_location_categories;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function saveTaskLocation($latitude, $longitude,
	$street_number, $street_name, $suburb, $postcode, $state, $country,
	$landmark_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::saveTaskLocation() --> Enter');
		sfContext::getInstance()->getLogger()->debug(sprintf('latitude: %f, longitude: %f,
							street_number: %d, street_name: %s, suburb: %s, postcode: %s, state: %s, country: %s,
							landmark_id: %d', $latitude, $longitude,
		$street_number, $street_name, $suburb, $postcode, $state, $country,
		$landmark_id));
		if($landmark_id == 0) {
			// if there is no associated landmark, json sends 0 instead of null. therefore, we need to make it null
			$landmark_id = null;
		}
		$result = $this->doSaveTaskLocation(null, $latitude, $longitude,
		$street_number, $street_name, $suburb, $postcode, $state, $country,
		$landmark_id);
		sfContext::getInstance()->getLogger()->debug('UserInformation::saveTaskLocation() --> Exit');
		return $result;
	}

	private function doSaveTaskLocation($task_location_id, $latitude, $longitude,
	$street_number, $street_name, $suburb, $postcode, $state, $country,
	$landmark_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::doSaveTaskLocation() --> Enter');
		if($task_location_id) {
			$task_location = TaskLocationPeer::retrieveByPK($task_location_id);
		} else {
			$task_location = new TaskLocation();
		}

		$task_location->setLatitude($latitude);
		$task_location->setLongitude($longitude);
		$task_location->setStreetNumber($street_number);
		$task_location->setStreetName($street_name);
		$task_location->setSuburb($suburb);
		$task_location->setPostcode($postcode);
		$task_location->setState($state);
		$task_location->setCountry($country);
		$task_location->setLandmarkId($landmark_id);

		try {
			$task_location->save();
		} catch (Exception $e) {
			return '-1';
		}

		$id = $task_location->getId();
		sfContext::getInstance()->getLogger()->debug('UserInformation::doSaveTaskLocation() --> Exit');
		return $id;
	}

	public function updateTaskLocation($task_location_id, $latitude, $longitude,
	$street_number, $street_name, $suburb, $postcode, $state, $country,
	$landmark_id) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::updateTaskLocation() --> Enter');
		sfContext::getInstance()->getLogger()->debug(sprintf('task_location_id: %d, latitude: %f, longitude: %f,
							street_number: %d, street_name: %s, suburb: %s, postcode: %s, state: %s, country: %s,
							landmark_id: %d', $task_location_id, $latitude, $longitude,
		$street_number, $street_name, $suburb, $postcode, $state, $country,
		$landmark_id));
		sfContext::getInstance()->getLogger()->debug('UserInformation::updateTaskLocation() --> Exit');
		if($landmark_id == 0) {
			// if there is no associated landmark, json sends 0 instead of null. therefore, we need to make it null
			$landmark_id = null;
		}
		return $this->doSaveTaskLocation($task_location_id, $latitude, $longitude,
		$street_number, $street_name, $suburb, $postcode, $state, $country,
		$landmark_id);
	}

	public function saveTask($name, $description, $type_id, $location_id,
	$year, $month, $day, $hour, $minute,
	$user_name, $priority_id, $ampm) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::saveTask() --> Enter');
		sfContext::getInstance()->getLogger()->debug(sprintf('name: %s, description: %s, type_id: %d, location_id: %d,
							 year: %d, month: %d, day: %d, hour: %d, minute: %d,
							 user_name: %s, priority_id: %d, ampm: %s', $name, $description, $type_id, $location_id,
		$year, $month, $day, $hour, $minute,
		$user_name, $priority_id, $ampm));
		return $this->doSaveTask(null, $name, $description, $type_id, $location_id,
		$year, $month, $day, $hour, $minute,
		$user_name, $priority_id, $ampm);
		sfContext::getInstance()->getLogger()->debug('UserInformation::saveTask() --> Exit');
	}

	public function updateTask($task_id, $name, $description, $type_id, $location_id,
	$year, $month, $day, $hour, $minute,
	$priority_id, $ampm) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::updateTask() --> Enter');
		sfContext::getInstance()->getLogger()->debug(sprintf('task_id: %d, name: %s, description: %s, type_id: %d, location_id: %d,
							 year: %d, month: %d, day: %d, hour: %d, minute: %d,
							 priority_id: %d, ampm: %s', $task_id, $name, $description, $type_id, $location_id,
		$year, $month, $day, $hour, $minute,
		$priority_id, $ampm));
		sfContext::getInstance()->getLogger()->debug('UserInformation::updateTask() --> Exit');
		return $this->doSaveTask($task_id, $name, $description, $type_id, $location_id,
		$year, $month, $day, $hour, $minute,
		null, $priority_id, $ampm);
	}

	private function doSaveTask($task_id, $name, $description, $type_id, $location_id,
	$year, $month, $day, $hour, $minute,
	$user_name, $priority_id, $ampm) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::doSaveTask() --> Enter');

		if($task_id) {
			$task = TaskPeer::retrieveByPK($task_id);
		} else {
			$task = new Task();
			$user = sfGuardUserPeer::retrieveByUsername($user_name);
			$user_id = $user->getId();
			$task->setUserId($user_id);
			sfContext::getInstance()->getLogger()->debug('doSaveTask()->user id: '. $user_id);
		}

		$task->setName($name);
		$task->setDescription($description);
		$task->setTypeId($type_id);
		$task->setLocationId($location_id);
		$task->setYear($year);
		$task->setMonth($month);
		$task->setDay($day);
		$task->setHour($hour);
		$task->setMinute($minute);
		$task->setPriorityId($priority_id);
		$task->setAmpm($ampm);

		try {
			$task->save();
		} catch (Exception $e) {
			return '-1';
		}

		$id = $task->getId();
		sfContext::getInstance()->getLogger()->debug('UserInformation::doSaveTask() --> Exit');
		return $id;
	}

	/**
	 * An user validation public method.
	 *
	 *
	 * @param string $user
	 * @param string $password
	 * @return mixed
	 */
	public function checkUsernameAndPassword($username, $password) {
		sfContext::getInstance()->getLogger()->debug('UserInformation::checkUsernameAndPassword() --> Enter');
		sfContext::getInstance()->getLogger()->debug('UserInformation::checkUsernameAndPassword() --> username: '. $username .'password: '. $password);
		$user = sfGuardUserPeer::retrieveByUsername($username);

		if(!$user) return 'false';

		if($user->checkPassword($password) == 1) {
			return 'true';
		} else {
			return 'false';
		}
		sfContext::getInstance()->getLogger()->debug('UserInformation::checkUsernameAndPassword() --> Exit');
	}

	public function getAllPostcodesAndSuburbs() {
		sfContext::getInstance()->getLogger()->debug('UserInformation::getAllPostcodesAndSuburbs() --> Enter');
		try{
			$postcode_suburbs = PostcodeSuburbPeer::doSelect(new Criteria());

			if(!$postcode_suburbs) return json_encode("0");

			$php_postcode_suburbs = array();
			$index = 0;
			
			foreach($postcode_suburbs as $postcode_suburb) {
				$php_postcode_suburb = array('postcode' => $postcode_suburb->getPostcode(),
							'suburb' => $postcode_suburb->getSuburb()
				);

				$php_postcode_suburbs[$index++] = $php_postcode_suburb;
			}

			$json_postcode_suburbs = JSONHelper::array2json($php_postcode_suburbs);
			sfContext::getInstance()->getLogger()->debug('UserInformation::getAllPostcodesAndSuburbs() --> Exit');
			return $json_postcode_suburbs;
		}catch (Exception $e) {
			return '-1';
		}
	}

	public function getCustomLandmarksByUsername($user_name)
	{
		sfContext::getInstance()->getLogger()->debug('UserInformation::getCustomLandmarksByUser() --> Enter');
		try{
			$user = sfGuardUserPeer::retrieveByUsername($user_name);

			if(!$user) return json_encode("0");

			$userLandmarks = $user->getUserLandmarksJoinLandmark();

			if($userLandmarks)
			{
				$php_landmarks = array();
				$index = 0;

				foreach($userLandmarks as $userLandmark)
				{
					$landmark = $userLandmark->getLandmark();

					$streetNumberName = $this->getStreetNumberName($landmark->getStreet());
					$category = $landmark->getLandmarkCategory();

					$php_landmark = array(
					'id' => $landmark->getId(),
					'name' => $landmark->getName(),
					'streetNumber' => $streetNumberName[0],
					'streetName' => $streetNumberName[1], /// road is changed to street name and number
					'suburb' => $landmark->getSuburb(),
					'postcode' => $landmark->getPostcode(),
					'state' => $landmark->getState(),
					'country' => 'Australia', // THIS VALUE IS HARDCODED FOR THE TIME BEING
					'latitude' => $landmark->getLatitude(),
					'longitude' => $landmark->getLongitude(),
					'categoryId' => $landmark->getCategoryId(),
					'categoryName' => $category->getName()
					);

					$php_landmarks[$index++] = $php_landmark;
				}
				$json_landmarks = JSONHelper::array2json($php_landmarks);
				sfContext::getInstance()->getLogger()->debug('UserInformation::getCustomLandmarksByUser() --> Exit');
				return $json_landmarks;
			}
			else
			{
				return json_encode("0");
			}
		}catch (Exception $e) {
			return '-1';
		}
		//return json_encode("0"); /// select operation generate empty result
			
	}


	public function doSaveCustomLandmark($user_name, $name, $streetNo, $street, $suburb, $state, $country,
	$postcode, $latitude, $longitude, $landmarkCategoryId, $description)
	{
		$user = sfGuardUserPeer::retrieveByUsername($username);
		//check valid user

		$user_id = $user->getId();

		$landmark = new Landmark();

		$landmark_id = $landmark->getId();

		$landmark->setName($name);
		$landmark->setStreetNo($streetNo);
		$landmark->setStreet($street);
		$landmark->setSuburb($suburb);
		$landmark->setState($state);
		$landmark->setCountry($country);
		$landmark->setPostcode($postcode);
		$landmark->setLatitude($latitude);
		$landmark->setLongiture($longitude);
		$landmark->setCategoryId( $landmarkCategoryId);
		$landmark->setDescription($description);
		try {
			$landmark->save();
		} catch (Exception $e) {
			return '-1';
		}

		$userLandmark = new UserLandmark();
		$userLandmark->setUserId($user_id);
		$userLandmark->setLandmarkId($landmark_id);

		try {
			$landmark->save();
		} catch (Exception $e) {
			return '-1';
		}

		return $userLandmark->getId();
	}
}
?>